package com.idealighter.game.games.horserace.struct;

import com.idealighter.game.games.common.AbstractSeat;
import com.idealighter.game.games.common.SeatState;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

/**
 * .. 赛马座位.
 * 
 * @date 2016年2月17日 上午10:46:55
 */
public class HorseRaceSeat extends AbstractSeat {

  // 所属桌子
  public final HorseRaceTable table;

  // 是否是庄家
  public boolean dealer = false;

  // 当前下注的筹码
  public long betedchips = 0;
  // 当前结算筹码
  public long billChips = 0;

  // key 下注区域，value 对应筹码
  public final Map<Integer, Long> bets = new HashMap<>();

  public int bankerNum;
  public int bankerScore;


  /**
   * 构造函数 .
   * 
   * @param order 位置.
   * @param table 桌子.
   */
  public HorseRaceSeat(int order, HorseRaceTable table) {
    super(order);
    this.table = table;
  }


  /**
   * 合计所有图标押注.
   * 
   * @return 总筹码.
   */
  public long totalBets() {
    long totalBets = 0;
    for (Long bet : bets.values()) {
      totalBets += bet;
    }

    return totalBets;
  }

  /**
   * 某个区域的下注.
   * 
   * @param area 区域.
   * @return 区域总投资.
   */
  public long totalAreaBets(int area) {
    long totalBets = 0;
    for (Entry<Integer, Long> bet : bets.entrySet()) {
      if (bet.getKey() == area) {
        totalBets += bet.getValue();
      }
    }
    return totalBets;
  }

  /**
   * 重置座位数据.
   */
  public void reset() {
    this.dealer = false;
    this.bets.clear();
    this.betedchips = 0;
    this.billChips = 0;
    this.totalChips = 0;
    this.state = SeatState.SEATED;
    this.bankerNum = 0;
    this.bankerScore = 0;
  }

  /**
   * 重置每次的游戏数据数据.
   */
  public void resetGameData() {
    this.bets.clear();
    this.billChips = 0;
  }

  /**
   * 重置座位数据除了totalChips.
   */
  public void resetButTotalChips() {
    long totalChips = this.totalChips;
    reset();
    this.totalChips = totalChips;
  }

  /**
   * 清空座位数据.
   */
  public void clear() {
    this.playerId = 0;
    this.playerName = null;
    reset();

    if (table.players().size() <= 1) {
      table.reset();
    }
  }

  @SuppressWarnings("unchecked")
  @Override
  public HorseRaceTable table() {
    return table;
  }
}
